# coding=utf-8
"""
备份数据到本地
234服务器 bkgitlab.sh的内容

cd /home
tar -czvf gitlabbk.tar.gz gitlab pgsql

"""
import paramiko

import datetime

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.load_system_host_keys()
client.connect(hostname='192.168.1.234', port=22, username='root', password='111111')

stdin, stdout, stderr = client.exec_command('/home/bkgitlab.sh')
print(stdout.readlines())

print("tar gitlab done")
client.close()

# 2
client = paramiko.Transport(("192.168.1.234",22))
client.connect(username='root', password='111111')
sftp = paramiko.SFTPClient.from_transport(client)  # sftp传输协议

src = "/home/gitlabbk.tar.gz"

curDate = datetime.date.today()
strCurDate = curDate.strftime('%Y%m%d')
des = "/media/PROG/gitlab223_bk/gitlabbk-" + strCurDate + ".tar.gz"

sftp.get(src, des)

print("backup gitlab done")
client.close()